﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using System.Linq;
using System.Collections.ObjectModel;
using A_ManageUsers.Services.Security;
namespace A_ManageUsers.DataModels
{
    public class RoleAbility:DataModelBase
    {
        public static ObservableCollection<RoleAbility> GetRolesAbilities(int RoleID, int[] Abs)
        {
            return new ObservableCollection<RoleAbility>(Abilities.AbilitiesL.Select((A) => new RoleAbility(RoleID, Abs.Contains(A.ID), A)));
        }

        public RoleAbility(int RoleID, bool Enabled, Ability Ab)
        {
            this.RoleID = RoleID;
            this._Enabled = Enabled;
            this.originalEnabled = Enabled;
            A = Ab;
            Ab.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Ab_PropertyChanged);
        }
        Ability A;
        void Ab_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ID")
            {
                OnPropertyChanged("AbilityID");
            }
            else if (e.PropertyName == "Name")
            {
                OnPropertyChanged("AbilityName");
            }
        }

        public int RoleID { get; set; }

        private bool originalEnabled;
        private bool _Enabled;
        public bool Enabled
        {
            get { return _Enabled; }
            set
            {
                if (_Enabled != value)
                {
                    base.SetRevertValue("Enabled", _Enabled);
                    _Enabled = value;
                    base.SetComitAction("Enabled", value, (DMB, PN, V) =>
                        {
                            UserManagementClient UMC = ServiceConfig.NewUserManagementClient();
                            if (value == originalEnabled)
                            { 
                                //In the event that the value hasn't changed we don't want to bother sending anything to the server.
                            }
                            else if (value)
                            {
                                UMC.AddAbilityToRoleAsync(RoleID, AbilityID);
                            }
                            else
                            {
                                UMC.RemoveAbilityFromRoleAsync(RoleID, AbilityID);
                            }
                            originalEnabled = value;
                        });
                    OnPropertyChanged("Enabled");
                }
            }
        }

        public int AbilityID { get { return A.ID; } }
        public string AbilityName { get { return A.Name; } }

        protected override void ComitNewObject()
        {
            
        }
    }
}
